package com.example.rotateimageview.scrollView

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout
import android.widget.Scroller

/**
 * author  : Liushuai
 * time    : 2021/10/10 20:33
 * desc    :
 */
class ScrollerLinearLayout@JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet?=null
) : LinearLayout(context,attributeSet) {

    private val mScroller :Scroller = Scroller(context,LinearInterpolator())

    public fun startScroller(startX:Int,dx:Int) {
        mScroller.startScroll(startX,0,dx,0)
        invalidate()
    }


    //由draw调用的 而invalidate又会调用draw  mScroller相当于一个计算器了
    override fun computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.currX,mScroller.currY)
            invalidate()
        }
        //这个写在外面的话会一直在调用
//        invalidate()
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_MOVE) {
            return true
        }
        return false
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        //滑动
        //抬起
        return super.onTouchEvent(event)
    }
}